//
//  PCDApiGetLocation.m
//  PCDMPaasBank
//
//  Created by DK on 2021/3/8.
//  Copyright © 2021 Alibaba. All rights reserved.
//

#import "PCDApiGetLocation.h"
#import "PCDLocationConverter.h"

@interface PCDApiGetLocation()<CLLocationManagerDelegate>
@property (nonatomic,strong ) CLLocationManager *locationManager;//定位服务
@property (nonatomic,copy)    NSString *currentCity;//城市
@property (nonatomic,copy)    NSString *strLatitude;//经度
@property (nonatomic,copy)    NSString *strLongitude;//维度
@property (nonatomic,strong)  NSDictionary * dataDic;
@property (nonatomic,assign)  BOOL  LoaclAdress;
@end

#define PCD_DEFAULT_LONGTITUDE @"114.508729"
#define PCD_DEFAULT_LATITUDE @"38.036062"
#define PCD_DEFAULT_CITY @"石家庄市"
#define PCD_DEFAULT_COUNTRY @"中国"
#define PCD_DEFAULT_PROVINCE @"河北省"

@implementation PCDApiGetLocation

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback
{
    self.bsContext = context;
    self.dataDic=(NSDictionary*)data;
    [self locateBaidu_map];
}

- (void)locateBaidu_map {
    self.bsContext = self.bsContext;
    NSDictionary * dataDic= self.dataDic;
    __weak typeof(self) weakSelf = self;
    [[PCDLocateModel shareLocateInstance] baidu_locateWithCompletion:^(BMKLocation * _Nullable location, NSDictionary *locations, NSError *error) {
        NSString *strCallBack = [dataDic objectForKey:@"success"];
        NSString *failCallBack = [dataDic objectForKey:@"fail"];
        if (error) {
            evaluateJavaScriptOnMainThread(weakSelf, failCallBack, @"");
        }else{
            
            NSString *message = [NSString stringWithFormat:@"%@%@%@%@%@",locations[@"Province"],locations[@"City"],locations[@"District"],locations[@"Street"],locations[@"StreetNumber"]];

            NSString *locationStatus = @"true";
            if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {
                
                //定位功能可用
                locationStatus = @"true";
                
            }else if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {
        
                //定位不能用
                locationStatus = @"false";
            }
            NSString *strJson = [@{@"status":locationStatus,
                                   @"latitude":[PCDUtil safeString:locations[@"Latitude"]],
                                   @"longitude": [PCDUtil safeString:locations[@"Longitude"]],
                                   @"country":[PCDUtil safeString:locations[@"Country"]],
                                   @"province":[PCDUtil safeString:locations[@"Province"]],
                                   @"city":[PCDUtil safeString:locations[@"City"]],
                                   @"district":[PCDUtil safeString:locations[@"District"]],
                                   @"cityCode":[PCDUtil safeString:location.rgcData.adCode],
                                   @"address":message
                                 } JSONRepresentation];
            evaluateJavaScriptOnMainThread(weakSelf, strCallBack, strJson);
        }
    }];
}


@end
